package net.lahuobao.finance.service.impl.payrate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.lahuobao.finance.dao.AccreditMapper;
import net.lahuobao.finance.dao.AccreditRecordMapper;
import net.lahuobao.finance.entity.accredit.Accredit;
import net.lahuobao.finance.entity.accredit.AccreditRecord;
import net.lahuobao.finance.entity.accredit.EAccrualType;
import net.lahuobao.finance.entity.payrate.CargoOwnerPayRatesEntity;
import net.lahuobao.finance.entity.payratedetail.CargoOwnerPayRatesDetailEntity;
import net.lahuobao.finance.entity.steward.Steward;
import net.lahuobao.finance.service.impl.payratedetail.CargoOwnerPayRatesDetailServiceImpl;
import net.lahuobao.finance.service.payrate.CargoOwnerPayRatesServiceI;
import net.lahuobao.finance.service.payratedetail.CargoOwnerPayRatesDetailServiceI;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

@Service("cargoOwnerPayRatesService")
@Transactional
public class CargoOwnerPayRatesServiceImpl extends CommonServiceImpl implements CargoOwnerPayRatesServiceI {
	@Autowired
	private CargoOwnerPayRatesDetailServiceI cargoOwnerPayRatesDetailService;
	@Autowired
	private AccreditMapper accreditDao;
	@Autowired
	private AccreditRecordMapper accreditRecordDao;
	
	@Override
	public void updateSteward(Steward steward, CargoOwnerPayRatesDetailEntity detail,
			CargoOwnerPayRatesEntity payRate) {
		//更新或者保存物流管家信息
		this.saveOrUpdate(steward);
		if(null!=detail){
			//更新或者保存费率明细
			cargoOwnerPayRatesDetailService.saveOrUpdate(detail);
			/*List<CargoOwnerPayRatesDetailEntity> detailList=cargoOwnerPayRatesDetailService.findByProperty(CargoOwnerPayRatesDetailEntity.class, "organizationId", detail.getOrganizationId());
			if(detailList.size()>0){//一个货主只能有一条记录
				CargoOwnerPayRatesDetailEntity detail1 = detailList.get(0);
				detail.setId(detail1.getId());
				this.getSession().delete(detail1);
				this.getSession().flush();
				cargoOwnerPayRatesDetailService.updateEntitie(detail);
			}else{
				cargoOwnerPayRatesDetailService.save(detail);
			}*/
		}
		if(null!=payRate){
			//更新或者保存组织机构
		/*	List<CargoOwnerPayRatesEntity> payRateList= this.findByProperty(CargoOwnerPayRatesEntity.class, "organizationId", steward.getOrganizationId());
			if(payRateList.size()>0){//一个货主只能有一条记录
				CargoOwnerPayRatesEntity payRate1 = payRateList.get(0);
				payRate.setId(payRate1.getId());
				//this.getSession().delete(payRate);
				//this.getSession().merge(payRate1);
				this.getSession().delete(payRate1);
				this.getSession().flush();

				updateEntitie(payRate);
			}else{
				save(payRate);
			}*/
			saveOrUpdate(payRate);
		}
		
		

        // 获取物流管家贷款账户
        Accredit accredit = null;
        List<Accredit> accreditList =this.findByProperty(Accredit.class, "organizationId", steward.getOrganizationId());
        for (Accredit sAccredit : accreditList) {
            if (sAccredit.getAccreditTypeId() == 1) // 物流管家贷款
            {
                accredit = sAccredit;
                break;
            }
        }

        // if (steward.getAllowPayment() == true || steward.getAllowUnify() == true) { // 允许贷款

        // 账户余额
        Integer paymentLimit = steward.getPaymentLimit();
        BigDecimal total = BigDecimal.valueOf(paymentLimit);
        // 利息类型
        EAccrualType accrualType = EAccrualType.DAILY;
        if (steward.getAllowDaily() == true) {
            accrualType = EAccrualType.DAILY;
        }

        if (steward.getAllowMonthly() == true) {
            accrualType = EAccrualType.MONTHLY;
        }

        // 允许贷款
        if (accredit == null) {
            accredit = new Accredit();
            accredit.setOrganizationId(steward.getOrganizationId());
            accredit.setAccreditTypeId(1); // 物流管家小贷

            accredit.setAccrualType(accrualType.getValue());
            accredit.setAccrualRate(BigDecimal.ZERO);
            accredit.setTotal(total);
            accredit.setUsable(total);
            accredit.setBill(BigDecimal.ZERO);
            accredit.setStatus(steward.getStewardState());
            this.save(accredit);
        } else {
            accredit.setAccrualType(accrualType.getValue());
            accredit.setAccrualRate(BigDecimal.ZERO);
            accredit.setTotal(total);
            accredit.setStatus(steward.getStewardState());
          //  this.updateEntitie(accredit);
            // 刷新 账户余额
            Integer accreditId = accredit.getAccreditId();
            
            // 获取当前余额 sum
            ArrayList<AccreditRecord> accreditRecordList = accreditRecordDao.selectALlCarriage(accreditId);
            BigDecimal bill = BigDecimal.ZERO;
            if (accreditRecordList.get(0) != null) {
                bill = accreditRecordList.get(0).getCarriage();
            }

            // 获取信用账户信息
            Accredit accredit1 = accreditDao.selectByPrimaryKey(accreditId);
            BigDecimal total1 = accredit1.getTotal();
            accredit.setBill(bill);
            BigDecimal usable = total1.subtract(bill);
            accredit.setUsable(usable);
            this.updateEntitie(accredit);
        }

	}
	
}